package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.redis.RedisPrefixConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.workorder.pojo.dto.WarehouseOriDeleteBatchDTO;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.WarehouseOristockDao;
import com.xbongbong.saas.domain.entity.WarehouseOristockEntity;
import com.xbongbong.saas.model.WarehouseOristockModel;
import org.elasticsearch.action.support.WriteRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Program xbb-pro
 * @Description
 * @Author caoyy
 * @Date 2019/10/17
 **/
@Service
public class WarehouseOristockModelImpl implements WarehouseOristockModel {
    @Resource
    private WarehouseOristockDao warehouseOristockDao;
    @Resource
    private PaasEsModel paasEsModel;

    @Override
    public Integer insert(WarehouseOristockEntity entity) throws XbbException {

        warehouseOristockDao.insert(entity);
        Integer insertId = entity.getId();
        PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
        BeanUtil.copyProperties(entity, paasFormDataEntity);
        paasFormDataEntity.setId(insertId.longValue());
        paasFormDataEntity.setData(null);
        InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_ORISTOCK);
        paasEsModel.insert(insertDTO);
        return insertId;
    }

    @Override
    public Integer update(WarehouseOristockEntity entity) throws XbbException {
        return null;
    }

    @Override
    public List<WarehouseOristockEntity> findEntitys(Map<String, Object> param) {
        return warehouseOristockDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return null;
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }


    @Override
    public List<Integer> deleteByWarehouseId(String corpid, List<Long> dataIdIn) throws XbbException {
        Map<String,Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(StringConstant.CORPID,corpid);
        param.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
        param.put("warehouseIdIn",dataIdIn);
        param.put("columns","id");
        List<WarehouseOristockEntity> list = warehouseOristockDao.findEntitys(param);
        if (list.isEmpty()) {
            return new ArrayList<>();
        }
        List<Integer> idIn = new ArrayList<>();
        List<InsertDTO> updateDTOList = new ArrayList<>();
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("del",1);
        list.forEach(item -> {
            idIn.add(item.getId());
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setWaitUntil(WriteRequest.RefreshPolicy.NONE);
            insertDTO.setEsId(corpid + "_" + item.getId());
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_ORISTOCK);
            insertDTO.setSource(map);
            updateDTOList.add(insertDTO);
        });
        warehouseOristockDao.deleteByIdIn(corpid, idIn);
        paasEsModel.updateBatch(updateDTOList ,WriteRequest.RefreshPolicy.WAIT_UNTIL, false);
        return idIn;
    }

    @Override
    public Set<Long> getWarehouseIdUnion(Set<Long> warehouseIdList, String corpid) {
        return warehouseOristockDao.getWarehouseIdUnion(warehouseIdList, corpid);
    }
}
